package com.wwwarehouse.taskcenter.adapter.job_point;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.AlignContent;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayout;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5HavedUnitResponseBean;
import com.wwwarehouse.taskcenter.fragment.job_point.step5.ChooseJobUnitHavedUnitDetailFragment5;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * 定义实物资源的主页面的adapter。
 */

public class TaskPointUnitHavedUnitAdapter extends RecyclerView.Adapter<TaskPointUnitHavedUnitAdapter.ViewHolder> {
    private ArrayList<AddJobPointStep5HavedUnitResponseBean> lists = new ArrayList<>();
    private ChooseJobUnitHavedUnitDetailFragment5 fragment5;
    private Context mContext;
    private View view;
    private int itemHeight;
    TextView textView;


    private void caculateItemHeight() {
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);

        itemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext, 155)) / 2;

    }

    public TaskPointUnitHavedUnitAdapter(ArrayList<AddJobPointStep5HavedUnitResponseBean> lists, Context mContext, ChooseJobUnitHavedUnitDetailFragment5 fragment) {
        this.lists = lists;
        this.mContext = mContext;
        this.fragment5 = fragment;
        caculateItemHeight();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_taskpoint_adapter_havedunit_5, parent, false);
        ViewHolder holder = new ViewHolder(view);

        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = itemHeight;
        view.setLayoutParams(params);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final AddJobPointStep5HavedUnitResponseBean bean = lists.get(position);

        if(bean.isSelected()){
            holder.mAddPoinHadunitAdapterItemAddImg.setBackgroundResource(R.drawable.radio_checked);
        }else{
            holder.mAddPoinHadunitAdapterItemAddImg.setBackgroundResource(R.drawable.radio_unchecked);
        }


        holder.mAddPoinHadunitAdapterItemAddLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bean.isSelected()){
                    bean.setSelected(false);
                }else{
                    bean.setSelected(true);
                }
                notifyDataSetChanged();
                EventBus.getDefault().post(bean);
            }
        });

        holder.mAddPoinHadunitAdapterItemStatebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment5.goToDetailPage(bean);
            }
        });

        holder.mAddPoinHadunitAdapterItemTitle.setText(bean.getJbUnitName());

        //布局方向
        holder.mAddPoinHadunitAdapterItemFlexlayout.setFlexDirection(FlexDirection.ROW);
        //正常换行
        holder.mAddPoinHadunitAdapterItemFlexlayout.setFlexWrap(FlexWrap.WRAP);
        //副轴起点对齐
        holder.mAddPoinHadunitAdapterItemFlexlayout.setAlignContent(AlignContent.FLEX_START);

        holder.mAddPoinHadunitAdapterItemFlexlayout.removeAllViews();
        for(AddJobPointStep5HavedUnitResponseBean.JbUnitFactorsBean jbUnitFactorsBean : bean.getJbUnitFactors()){
            textView = new TextView(mContext);
            if(StringUtils.isNullString(jbUnitFactorsBean.getMenberNumUnit())){
                textView.setText("  " + jbUnitFactorsBean.getMemberName() + "("+  jbUnitFactorsBean.getMemberNum() + ")  ");
            }else{
                textView.setText("  " + jbUnitFactorsBean.getMemberName() + "("+  jbUnitFactorsBean.getMemberNum() + jbUnitFactorsBean.getMenberNumUnit() +")  ");
            }

            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textView.setHeight(ConvertUtils.dip2px(mContext,20));
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setTextColor(mContext.getResources().getColor(R.color.common_color_c7_96999e));
            holder.mAddPoinHadunitAdapterItemFlexlayout.addView(textView);
        }

    }


    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mAddPoinHadunitAdapterItemTitle;
        private ImageView mAddPoinHadunitAdapterItemAddImg;
        private TextView mAddPoinHadunitAdapterItemAddTxt;
        private LinearLayout mAddPoinHadunitAdapterItemAddLin;
        private FlexboxLayout mAddPoinHadunitAdapterItemFlexlayout;
        private StateButton mAddPoinHadunitAdapterItemStatebtn;
        private LinearLayout mAddPoinHadunitAdapterItemRel;

        public ViewHolder(View itemView) {
            super(itemView);
            mAddPoinHadunitAdapterItemTitle = (TextView) itemView.findViewById(R.id.add_poin_hadunit_adapter_item_title);
            mAddPoinHadunitAdapterItemAddImg = (ImageView)itemView.findViewById(R.id.add_poin_hadunit_adapter_item_add_img);
            mAddPoinHadunitAdapterItemAddTxt = (TextView)itemView.findViewById(R.id.add_poin_hadunit_adapter_item_add_txt);
            mAddPoinHadunitAdapterItemFlexlayout = (FlexboxLayout) itemView.findViewById(R.id.add_poin_hadunit_adapter_item_flexlayout);
            mAddPoinHadunitAdapterItemStatebtn = (StateButton) itemView.findViewById(R.id.add_poin_hadunit_adapter_item_statebtn);
            mAddPoinHadunitAdapterItemRel = (LinearLayout)itemView.findViewById(R.id.add_poin_hadunit_adapter_item_rel);
            mAddPoinHadunitAdapterItemAddLin = (LinearLayout)itemView.findViewById(R.id.add_poin_hadunit_adapter_item_add_lin);

        }
    }


}
